package com.power.flowable.controller;

import com.power.flowable.api.IPersonalFeign;
import com.power.flowable.api.IProcessStatisticsQueryFeign;
import com.power.flowable.model.pageinfo.TaskPageInfo;
import feign.Response;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

@RestController
public class PersonalController {

    @Resource
    private IPersonalFeign iPersonalFeign;

    @Resource
    private IProcessStatisticsQueryFeign iProcessStatisticsQueryFeign;

    @ApiOperation("导出申请查询列表")
    @PostMapping("/flow/exportApplyQueryOutpu")
    public void applyQueryOutput(HttpServletResponse httpServletResponse, @RequestBody Map<String, Object> params){
        Response response = iProcessStatisticsQueryFeign.applyQueryOutput(params);
        Response.Body body = response.body();
        try (InputStream inputStream = body.asInputStream(); OutputStream outputStream = httpServletResponse.getOutputStream())
        {
            httpServletResponse.setHeader(HttpHeaders.CONTENT_DISPOSITION, response.headers().get(HttpHeaders.CONTENT_DISPOSITION).stream().findFirst().get());
            httpServletResponse.setContentType("application/x-xls");
            IOUtils.copy(inputStream, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("导出待处理任务列表")
    @PostMapping("/flow/export")
    public void output(HttpServletResponse httpServletResponse, @RequestParam String userid, @RequestBody TaskPageInfo pageinfo){
        Response response = iPersonalFeign.output(userid, pageinfo);
        Response.Body body = response.body();
        try (InputStream inputStream = body.asInputStream(); OutputStream outputStream = httpServletResponse.getOutputStream())
        {
            httpServletResponse.setHeader(HttpHeaders.CONTENT_DISPOSITION, response.headers().get(HttpHeaders.CONTENT_DISPOSITION).stream().findFirst().get());
            httpServletResponse.setContentType("application/x-xls");
            IOUtils.copy(inputStream, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/flow/viewFlowChart")
    public void viewFlowChart(HttpServletResponse response, @RequestParam(name = "processinstanceid", required = true) String processinstanceid) throws IOException {

        Response rep = iPersonalFeign.viewProcessDiagram(processinstanceid);

        Response.Body body = rep.body();
        InputStream in = body.asInputStream();

        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];
        int legth = 0;
        try {
            while ((legth = in.read(buf)) != -1) {
                out.write(buf, 0, legth);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

}
